package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressServiceImpl implements DeliveryAddressService {
    @Override
    public List<DeliveryAddress> listDeliveryAddressByUserId(String userId) {
        List<DeliveryAddress> list = new ArrayList<>();
        DeliveryAddressDaoImpl deliveryAddressDao = new DeliveryAddressDaoImpl();
        try{
            list = deliveryAddressDao.listDeliveryAddressByUserId(userId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }

    @Override
    public DeliveryAddress getDeliveryAddressById(Integer daId) {
        DeliveryAddress deliveryaddress = new DeliveryAddress();
        DeliveryAddressDaoImpl deliveryAddressDao = new DeliveryAddressDaoImpl();
        try{
            deliveryaddress = deliveryAddressDao.getDeliveryAddressById(daId);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return deliveryaddress;
    }

    @Override
    public int saveDeliveryAddress(String contactName, Integer contactSex, String contactTel, String address, String userId){
        int result = 0;
        try {
            DBUtil.beginTransaction();//开启事务
            DeliveryAddressDaoImpl deliveryAddressDao = new DeliveryAddressDaoImpl();
            result = deliveryAddressDao.saveDeliveryAddress(contactName, contactSex, contactTel, address, userId);
            DBUtil.commitTransaction();//提交事务
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();//回滚事务
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int updateDeliveryAddress(Integer daId, String contactName, Integer contactSex, String contactTel, String address, String userId){
        int result = 0;
        try {
            DBUtil.beginTransaction();
            DeliveryAddressDaoImpl deliveryAddressDao = new DeliveryAddressDaoImpl();
            result = deliveryAddressDao.updateDeliveryAddress(daId,contactName,contactSex,contactTel,address,userId);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }

    @Override
    public int removeDeliveryAddress(Integer daId){
        int result = 0;
        try {
            DBUtil.beginTransaction();
            DeliveryAddressDaoImpl deliveryAddressDao = new DeliveryAddressDaoImpl();
            result = deliveryAddressDao.removeDeliveryAddress(daId);
            DBUtil.commitTransaction();
        } catch (Exception e) {
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return result;
    }
}
